import 'dart:async';
import 'dart:math';
import 'package:app/util.dart';
import 'package:flutter/cupertino.dart' hide Interval;
import 'package:flutter/material.dart' hide Interval;
import 'package:k_chart/chart_style.dart';
import 'package:k_chart/chart_translations.dart';
import 'package:k_chart/entity/k_line_entity.dart';
import 'package:k_chart/k_chart_widget.dart';


class CenterView extends StatelessWidget{
  const CenterView({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Flexible(
      flex: 1,
      child: Flex(
        direction: Axis.vertical,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        verticalDirection: VerticalDirection.down,
        textDirection: TextDirection.rtl,
        children:  const [
          KlineState(),
          Forms(),
        ],
      ),
    );
  }

}

class KlineState extends StatefulWidget{
  const KlineState({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => KlineWeiget();

}

class KlineWeiget extends State<KlineState>{

  var data = <KLineEntity>[];
  ChartStyle chartStyle = ChartStyle();
  ChartColors chartColors = ChartColors();

  void initData(){
    // get_kline(1);
    for(var i=0;i<100;i++){
      data.add(
          KLineEntity.fromCustom(
            vol: Random().nextDouble()*100, open: Random().nextDouble()*10000, time: i, high:  Random().nextDouble()*10000, close: Random().nextDouble()*10000, low: Random().nextDouble()*10000,
          )
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    chartStyle.pointWidth = 20;
    chartStyle.topPadding = 50;
    chartStyle.bottomPadding = 50;
    initData();
    return Flexible(
      flex: 1,
      child:Flex(
        direction: Axis.vertical,
        children: [
          Flexible(
            flex: 1,
            child: CenterBar('add'),
          ),
          Flexible(
            flex: 15,
            child:
              KChartWidget(
                  data,// Required，Data must be an ordered list，(history=>now)
                  chartStyle, // Required for styling purposes
                  chartColors,// Required for styling purposes

                  isLine: false,// Decide whether it is k-line or time-sharing
                  mainState: MainState.BOLL,// Decide what the main view shows
                  secondaryState: SecondaryState.NONE,// Decide what the sub view shows
                  fixedLength: 2,// Displayed decimal precision
                  timeFormat: TimeFormat.YEAR_MONTH_DAY,
                  onLoadMore: (bool a) {},// Called when the data scrolls to the end. When a is true, it means the user is pulled to the end of the right side of the data. When a
                  // is false, it means the user is pulled to the end of the left side of the data.
                  maDayList: [5,10,20],// Display of MA,This parameter must be equal to DataUtil.calculate‘s maDayList
                  translations: kChartTranslations,// Graphic language
                  volHidden: true,// hide volume
                  showNowPrice: true,// show now price
                  isOnDrag: (isDrag){},// true is on Drag.Don't load data while Draging.
                  onSecondaryTap:(){},// on secondary rect taped.
                  isTrendLine: true // You can use Trendline by long-pressing and moving your finger after setting true to isTrendLine property.
              ),
          )
        ],
      ),
    );
  }

  Widget CenterBar(String type){
    return Flex(
      direction: Axis.horizontal,
      children: [
        IconButton(
          onPressed: () {},
          hoverColor: Colors.pink,
          icon: Icon(Icons.add),
          iconSize: 20,

        ),
        IconButton(
          onPressed: () {},
          hoverColor: Colors.pink,
          icon: Icon(Icons.add),
          iconSize: 20,

        )

      ],
    );
  }

}

class Forms extends StatefulWidget{
  const Forms({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _Forms();

}

class _Forms extends State<Forms>{
  @override
  Widget build(BuildContext context) {
    return Flexible(
      flex: 1,
      child: Text('111'),
    );
  }

}

